<template>
  <div class="container">

    <Header></Header>
     <!-- 中间LOGO -->
     <div class="centerLogo">
      <van-image
        width="134"
        height="45"
        src="//yanxuan.nosdn.127.net/static-union/164793255107785e.png"
      />
     </div>

    <!-- 表单 -->
    <div class="fromBox">
      <van-form  @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="phone"
            name="用户名"
            placeholder="请输入手机号"
            clearable
            :rules="[{ pattern, message: '请输入正确的手机号' }]"
          />
          <van-field
            v-model="code"
            clearable
            name="验证码"
            placeholder="请输入短信验证码"
            :rules="[{ required: true, message: '请填写短信验证码' }]"
          >
            <template #button>
              <van-button @click="sendCode" size="small" type="default">发送验证码</van-button>
            </template>
          </van-field>
          <van-field
            v-model="password"
            name="密码"
            placeholder="请输入密码"
            clearable
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <van-field
            v-model="repassword"
            :rules="[{ validator, message: '两次密码不一致,请重新输入' },{required: true, message: '请确认密码'}]"
            name="确认密码"
            placeholder="请再次输入密码"
            clearable
          />
        </van-cell-group>

        <div class="codeLoginBox">
          <span @click="toLogin" class="text">去登录</span>
          <van-button class="toCodeLogin" type="default">使用验证码登录</van-button>
        </div>

        <div style="margin: 16px;">
          <van-button  block type="danger" native-type="submit">
            注册
          </van-button>
        </div>

        <div name="协议" class="checkBox" >
          <van-checkbox v-model="isCheck" shape="square" name="checked"></van-checkbox>
          <span class="agreement">同意<a>《服务条款》</a>和<a>《隐私政策》</a>和<a>《网易严选服务协议》</a></span>
        </div>
      </van-form>
    </div>

  </div>
</template>

<script lang="ts" setup>
import {ref} from 'vue'
import { showSuccessToast, showFailToast } from 'vant';
import { reqPhoneCode, reqRegister } from '../../../api/center';
import { useRouter } from 'vue-router';


const phone = ref<string>('')
const pattern = /^1[3456789]\d{9}$/
const code = ref<string>('')
const password = ref<string>('')
const repassword = ref<string>('')
const isCheck = ref<boolean>(false)
const router = useRouter()

// 校验函数返回 true 表示校验通过，false 表示不通过
const validator = () => password.value === repassword.value;

const sendCode =async () => {
  if(pattern.test(phone.value)){
    let res = await reqPhoneCode(phone.value)
    code.value = res.data
  }
}


const onSubmit =async () => {
  // console.log('验证通过');
  if(!isCheck.value){
    showFailToast('请阅读并同意用户协议');
    return
  } 
    let res = await reqRegister({
      phone:phone.value,
      password:repassword.value,
      code:code.value
    })

   if(res.code == 200){
     showSuccessToast('注册成功');
     router.push('/phone')
   }else{
    showFailToast('账号已被注册');
   }
    

}

const toLogin = () => {
  router.push('/login')
}

</script>

<script lang="ts">
export default {
  name: 'phone'
}
</script>

<style lang="scss" scoped>
.container{
  padding-bottom:50px;
  .header{ 
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px; 
    height: 44px;
    font-size: 30px;
    border-bottom: 1px solid #ddd;
    .headerLogo{
      width:86px;
      height:30px;
    }
    .search{
      width:74px;
      height: 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
  .centerLogo{
    text-align:center;
    margin:28px 0;
  }
  .fromBox{
    padding: 0 5px;
    .codeLoginBox{
      display:flex;
      justify-content:space-between;
      align-items:center;
      margin-top:20px;
      .text{
        font-size:14px;
        margin-left:20px;
        color:#888;
      }
      .toCodeLogin{
        border:none;
        margin-right:5px;
      }
    }
    .checkBox{
      display:flex;
      align-items: flex-start;
      padding:0 16px;
      .agreement{
        font-size:14px;
        margin-left:10px;
        & a{
          color:#409EFF;
        }
      }
    }
    .van-cell{
      padding:10px 0;
      border-bottom: 1px solid #ddd;
      width:100%;
    }
  }
  .footer{
    font-size: 15px;
    display: flex;
    justify-content: center;
    margin-top: 80px;
  }
}

</style>
